<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Frederic Descamps <lefred@tiscali.be>
 * Copyright (C) 2011-2012 Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

function get_etats($where = '') {
	$requete = "SELECT * FROM etats ".$where." order by value DESC"; 
	$resultat = biblio_db_query($requete);
	if(!$resultat) {
		print "PROBLEME : ".biblio_db_error(); 
		return false;
	}
	$rc = array();
	while ($row = biblio_db_fetch_array($resultat)) {
		$id  = $row['id'];           
		$val = $row['etat'];
		$rc[$id] = $val;   
	}
	return $rc;
}

function get_borrower($book_id = 0) {
	$rc = false;
	if ($book_id == 0)
		return false;

	$requete = "SELECT * FROM prets2 WHERE livre_ID = $book_id AND date_in is null";
	$resultat = biblio_db_query($requete);
	if (!$resultat) 
		return false;

	while ($row = biblio_db_fetch_array($resultat))
    	$rc = $row;
	return $rc;
}

function get_book($id = 0) {
	$rc = false;
	if ($id == 0)
		return false;

	$requete = "SELECT * FROM livres where id = $id";
	$resultat = biblio_db_query($requete);
	if (!$resultat) 
		return false;

    while ($row = biblio_db_fetch_array($resultat))
    	$rc = $row;
	return $rc;
}

function format_title($nom) {
	$title = strtolower(trim($nom));  
	$first = strtoupper( $title[0] );  
	$title[0] =  $first;
	return $title;  
}

function format_author($author = '') {
	$n = explode(",", $author);
	$c = count($n);
	if ($c < 2)
		return strtoupper($author);
	$rc = strtoupper(trim($n[0])).", ";
	for ($i = 1; $i < $c; $i++)
		if ($i == 1)
			$rc .= format_title($n[$i]);
		else
			$rc .= ", ".format_title($n[$i]);
	return $rc;
}

function get_genres($where = '') {
	if ($where == '')
	  $requete = "SELECT * FROM genres order by genre"; 
	else
		$requete = "SELECT * FROM genres ".$where." order by genre"; 
		
  if(!($resultat = biblio_db_query($requete)))
    return false;
  $rc = array();
	while ($row = biblio_db_fetch_array($resultat)) {      
    $id = $row['id'];           
    $rc[$id] = $row['genre'];
  }                                
  return $rc;
}

function add_genre($genre = '') {
	if ($genre == '')
		return false;
	$requete = "insert into genres (genre) values ('$genre')";	
	if(!biblio_db_query($requete) )
		return false;
	return biblio_db_insert_id();
}

function get_location_string($local ='', $biblio ='', $rangee = '') {
	if (!isset($local) || ($local == '') )
		return '';
	$rc = $local;
	if (!isset($biblio) || ($biblio == '') )
		return $rc;
	$rc .= "-->".$biblio;
	if (!isset($rangee) || ($rangee == '') )
		return $rc;
	return $rc."-->".$rangee;
}

function get_locations($where = '') {
	if ($where == '')
	  $requete = "SELECT * FROM locations order by local"; 
	else
		$requete = "SELECT * FROM locations ".$where." order by local"; 
	if(!($resultat = biblio_db_query($requete)))
	  return false;
	$rc = array();
	while ($row = biblio_db_fetch_array($resultat)) {      
		$id = $row['id'];           
		$local_l =  $row['local'];
		$biblio_l =  $row['biblio'];
		$rangee_l =  $row['rangee'];
		$value = get_location_string($local_l, $biblio_l, $rangee_l);
		$rc[$id] = $value;
	}
  return $rc;
}

function add_location($local ='', $biblio ='', $rangee = '') {
	if ($local == '')
		return false;
	$requete = "insert into locations (local,biblio,rangee) values ('$local','$biblio','$rangee')";
	if(!biblio_db_query($requete))
		return false;
	return biblio_db_insert_id();
}

function get_title() {
	global $dbConfig;
	return $dbConfig->get('title');
}

function get_logpath() {
	global $dbConfig;
	$p = $dbConfig->get('log_path');
	if (!empty($p))
		return $p;
	else
		return getBaseDir()."logs/";
}

function get_rootpwd() {
	global $dbConfig;
	return $dbConfig->get('root_pwd');
}

function get_coverspath() {
	global $dbConfig;
	$p = $dbConfig->get('covers_path');
	if (!empty($p))
		return $p;
	else
		return getBaseDir()."images/covers/";
}

function get_picspath() {
	global $dbConfig;
	return $dbConfig->get('pics_path');
}

function get_language() {
	global $dbConfig;
	return $dbConfig->get('language');
}

function get_max_record() {
	global $dbConfig;
	return $dbConfig->get('max_record');
}

function getAmazonKeyId() {
	global $dbConfig;
	return $dbConfig->get('amazon_AccessKeyId');
}

function getAmazonSecret() {
	global $dbConfig;
	return $dbConfig->get('amazon_SecretAccessKey');
}

function getAmazonAssociateTag() {
	global $dbConfig;
	return $dbConfig->get('amazon_AssociateTag');
}

function isAmazonEnabled() {
	global $dbConfig;
	$enable = $dbConfig->get('amazon_enable');
	if (!$enable)
		return FALSE;
	if (empty($enable))
		return FALSE;
	if (strcasecmp($enable, 'false') == 0)
		return FALSE;
	return TRUE;
}

function return_isbn($rechcode)
{
  if (strlen($rechcode) == 13)
  {
    #recreate the isbn
    $rechcode=substr($rechcode,3);
    $rechcode=substr($rechcode,0,-1);
    $check=0;
    for ($i=0; $i<strlen($rechcode); $i++)
    {
       $check=$check+($i+1)*$rechcode[$i];
    }
    $rechcode=$rechcode . $check % 11;
  }
  return($rechcode);
}

function getBaseUri($uri) {
	// Subdirs of openbiblio
	static $openbiblio_subdirs = array('/barcode/', '/contrib/', '/css/', '/graphs/', '/images/', '/includes/', '/locale/');
	
	foreach ($openbiblio_subdirs as $dir) {
		$p = strpos($uri, $dir);
		if ($p !== FALSE)
			return substr($uri, 0, $p)."/";
	}
	$p = strrpos($uri, '/');
	if ($p !== FALSE)
		$uri = substr($uri, 0, $p);
	return $uri."/";
}

function getBaseUrl() {
	if (isset($_SERVER["HTTPS"])  && ($_SERVER["HTTPS"] == "on"))
		$baseUrl .= "https://";
	else
		$baseUrl = 'http://';
	$baseUrl .= $_SERVER["SERVER_NAME"];
	if ($_SERVER["SERVER_PORT"] != "80")
		$baseUrl .= ":".$_SERVER["SERVER_PORT"];
		return $baseUrl.getBaseUri($_SERVER['REQUEST_URI']);
}

function getBaseDir() {
	return getBaseUri($_SERVER['SCRIPT_FILENAME']);
}

?>
